# -*- coding:utf-8 -*-
from database.db_business import DbBusiness
from common.my_time import MyTime
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import os
from common.market_data import MarketData
from common.macro_data import MacroData


'''
网络数据
'''


class GongzhonghaoWenzhang:
    def __init__(self):
        self.business = DbBusiness()
        self.market = MarketData()
        self.macro = MacroData()
        date = MyTime.forward_relative_date(0)
        self.file = open('./out/gongzhonghao_' + date + ".html", 'w', encoding = 'utf-8')
        
        #self.file.write("文章包含截止当天12点和前一天的数据，访问底部链接，可了解详细信息。\n")
        relation_name = "中国官方|贸易战|疫情|联合国|美国政策|其它国外政策|国际关系|货币|国际事件|贸易|资源储备|海关|能源|经济指标|人口|财政|国债与地方债|水利|基建|反倾销|体育盛事|统计数据|大宗商品|自然灾害|指数|行业领头羊|个股动态|行业|水运|空运|农业|造纸|电机|电池|食品行业|玻璃行业|半导体行业|新能源行业|材料|金属|建筑行业|大型机械|食品添加剂|畜牧养殖|渔业|传统能源|非金属矿|酒行业|烟草行业|纺织业|化学原料制造|节假日"
        relation_id = "100|101|102|180|200|299|300|301|302|304|305|306|307|308|309|310|311|312|313|314|900|901|2100|2150|2200|2300|2301|2302|2303|2304|2305|2306|2307|2308|2309|2310|2311|2312|2313|2314|2315|2316|2317|2318|2319|2320|2321|2322|2323|2324|2325|2400"
        
        names = relation_name.split("|")
        ids = relation_id.split("|")
        self.name_id = {}
        for i in range (0, len(names)):
            self.name_id[ids[i]] = names[i]
        
    def __del__(self):
        self.file.write("如需了解资讯对应详情，可访问saybydata.com。\n")
        self.file.close()
    
    def get_data(self, diff, title):
        date = MyTime.forward_relative_date(diff)
        macro_datas = self.macro.query_macro_data(date)
        market_datas = self.market.query_market_data(date)
        
        html = "" #"<h1 style=\"font-size:48px\">" + title + "<h1>\n"
        data_type = ""
        for d in macro_datas:
            pos = d[2].find("...")
            if -1 != pos:
                continue
            if data_type != d[1]:
                html += "<hr />\n"
                html += "<h1 style=\"font-size:32px\">" + self.name_id[str(d[1])] + "<h1>\n"
                html += "<hr />\n"
                data_type = d[1]
            html += "<a href=" + d[3] + ">[" + d[4] +  "]" + d[2] + "</a><br/>\n"

        for d in market_datas:
            pos = d[2].find("...")
            if -1 != pos:
                continue
            # 个股动态
            if d[1] == 2301:
                continue
            if data_type != d[1]:
                html += "<hr />\n"
                html += "<h1 style=\"font-size:32px\">" + self.name_id[str(d[1])] + "<h1>\n"
                html += "<hr />\n"
                data_type = d[1]
            if d[3] != "":
                html += "<a href=" + d[3] + ">[" + d[4] +  "]" + d[2] + "</a><br/>\n"
            else:
                html += "<span>[" + d[4] +  "]" + d[2] + "</span><br/>\n"
        self.file.write(html)
        

if __name__ == '__main__':
    d = GongzhonghaoWenzhang()
    d.get_data(0, "今日最新")
    #d.get_data(1, "昨日回顾")

